class Surveys::TTestsManageController < ApplicationController
  # before_filter :auth
 
  # GET /tests
  # GET /tests.xml
    protect_from_forgery :except =>"create_question"
  def index
    @tests = Survey.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tests }
    end
  end

  # GET /tests/1
  # GET /tests/1.xml
  def show
    @test = Survey.find(params[:id])

  end

  # GET /tests/new
  # GET /tests/new.xml
  def new
    @test = Survey.new

    @trend_scales = Array.new
    (0..1).each {@trend_scales << TrendScale.new}

    @choices
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @test }
    end
  end

  # GET /tests/1/edit
  def edit
    @test = Survey.find(params[:id])
  end

  # POST /tests
  # POST /tests.xml
  def create
    @test = Survey.new(params[:test])
    #设置测试类型为倾向测试
    @test.t_type = TEST_TYPE_TREND
    @test.professional = false
    respond_to do |format|
      if @test.save
        #批量添加倾向选项
        params[:choice].each do |value|      
          c = Choice.new(value)
          c.survey_id = @test.id     
          c.save   
        end  
          
        flash[:notice] = 'Test was successfully created.'
        format.html { redirect_to(:controller => "surveys" ,:action => "index", :id => @test.id) }
        format.xml  { render :xml => @test, :status => :created, :location => @test }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @test.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /tests/1
  # PUT /tests/1.xml
  def update
    @test = Survey.find(params[:id])

    respond_to do |format|
      if @test.update_attributes(params[:test])
        flash[:notice] = 'Test was successfully updated.'
        format.html { redirect_to(@test) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @test.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /tests/1
  # DELETE /tests/1.xml
  def destroy
    @test = Survey.find(params[:id])
    @test.destroy

    respond_to do |format|
      format.html { redirect_to(:action => "index") }
      format.xml  { head :ok }
    end
  end
  
  #添加题目
  def new_question
    @test = Survey.find(params[:id])
    @question =Question.new
    respond_to do |format|
      format.html 
      format.xml  { render :xml => @test }
    end
  end
  
  #保存题目
  def create_question
    @test = Survey.find(params[:t_id])
    q = Question.new(params[:question])
    q.survey = @test      
    q.save
    if params["choice.content"]
         i = 0;
         params["choice.content"].each do |content|
           choice = Choice.new(:content => content, :value => params["choice.value"][i])
           choice.question = q
           choice.save 
          i += 1
         end
    end
    flash[:notice] = 'Question添加成功.'
      respond_to do |format|
        format.html { render :partial => "questions"}
      end
   # respond_to do |format|
    #  begin
        #批量保存问题
    #    params["question"].each do |value|   
    #      q = Question.new(value)
     #     q.test = @test     
     #     q.save!
     #   end
      #  flash[:notice] = '题目添加成功.'
      #  format.html { redirect_to(:action => "index") }
     # rescue
      #  format.html { render :action => "new_question" }
     # end
   # end
  end
  
  #添加倾向
  def new_trend_scale
    @trend_scale = TrendScale.new
    @test_id = params[:id]
  end
  
  #保存倾向
  def create_trend_scale
    @test = Survey.find(params[:test_id])
    @trend_scale = TrendScale.new(params[:trend_scale])
    @trend_scale.survey = @test
    
    respond_to do |format|
      if @trend_scale.save
        flash[:notice] = "<span style='color:green'>添加成功！您可以继续添加.</span>"
        #flash[:test_id] = params[:test_id]
        format.html { redirect_to(:action => "new_trend_scale", :id => params[:test_id]) }
      else
        flash[:notice] = "<span style='color:red'>保存失败.请再次尝试.</span>"        
        flash[:test_id] = params[:test_id]
        format.html { redirect_to(:action => "new_trend_scale", :id => params[:test_id]) }
      end

    end
  end

end







